package com.jwdf.uda.view.prompt;

import android.content.Context;
import android.content.DialogInterface;
import android.databinding.DataBindingUtil;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.jwdf.uda.R;
import com.jwdf.uda.databinding.ViewPromptHomeBinding;
import com.jwdf.uda.utils.DisplayUtils;
import com.jwdf.uda.utils.PromptUtil;
import com.jwdf.uda.view.dialog.BaseDialog;

/**
 * 类说明：
 */
public class HomePrompt extends BaseDialog implements DialogInterface.OnDismissListener {
    private ViewPromptHomeBinding mBinding;
    private int avatarPaddingTop;
    private int deviceWidth;
    private int deviceHeight;
    private boolean mCancelable;
    private View classifyGalleryView;
    private int tabBottom = 45;

    public HomePrompt(Context context, View avatarView) {
        super(context);
        this.classifyGalleryView = avatarView;
        this.deviceWidth = DisplayUtils.getDeviceWidth(context);
        this.deviceHeight = DisplayUtils.getDeviceHeight(context);
    }

    public HomePrompt(Context context, View avatarView, boolean cancelable) {
        this(context, avatarView);
        mCancelable = cancelable;
    }

    private Handler handler = new Handler();
    private Runnable mAvatarRunnable = new Runnable() {
        @Override
        public void run() {
            int[] location = new int[2];
            classifyGalleryView.getLocationOnScreen(location);
            location[1] -= DisplayUtils.getStatusBarHeight(getContext());
            avatarPaddingTop = location[1] + DisplayUtils.dip2px(getContext(), 10);

            mBinding.ivAvatar.setVisibility(View.VISIBLE);
            mBinding.ivAvatarTxt.setVisibility(View.VISIBLE);
            mBinding.ivAvatar.setPadding(DisplayUtils.dip2px(getContext(), 25), avatarPaddingTop, 0, 0);
            mBinding.ivAvatarTxt.setPadding(10, avatarPaddingTop, 0, 0);
        }
    };

    private Runnable mRecommendRunnable = new Runnable() {
        @Override
        public void run() {
//            int imageWidth = (deviceWidth - DisplayUtils.dip2px(getContext(), 20))/3;
//            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imageWidth, imageWidth);
//            mBinding.ivRecommend.setLayoutParams(params);
            int recommendPaddingLeft = DisplayUtils.dip2px(getContext(), 10);
//            int recommendPaddingTop = deviceHeight - imageWidth - DisplayUtils.dip2px(getContext(), tabBottom) ;

            mBinding.ivRecommend.setVisibility(View.VISIBLE);
            mBinding.ivRecommendTxt.setVisibility(View.VISIBLE);
            mBinding.ivRecommend.setPadding(recommendPaddingLeft, 0, 0, DisplayUtils.dip2px(getContext(), tabBottom + 15));
            mBinding.ivRecommendTxt.setPadding(recommendPaddingLeft, 0, 0, 10);
        }
    };

    private Runnable mDiscoverRunnable = new Runnable() {
        @Override
        public void run() {
            int discoverPaddingLeft = 3 * deviceWidth / 8 - PromptUtil.getMeasuredWidth(mBinding.ivDiscover) / 2;
            int discoverPaddingBottom = DisplayUtils.dip2px(getContext(), tabBottom) / 2 - PromptUtil.getMeasuredHeight(mBinding.ivDiscover) / 2;
            mBinding.ivDiscover.setVisibility(View.VISIBLE);
            mBinding.ivDiscoverTxt.setVisibility(View.VISIBLE);
            mBinding.ivDiscover.setPadding(discoverPaddingLeft, 0, 0, discoverPaddingBottom);
            mBinding.ivDiscoverTxt.setPadding(discoverPaddingLeft, 0, 0, 10);
        }
    };

    private Runnable mGuideRunnable = new Runnable() {
        @Override
        public void run() {
            int guidePaddingLeft = 5 * deviceWidth / 8 - PromptUtil.getMeasuredWidth(mBinding.ivGuide) / 2;
            int guidePaddingBottom = DisplayUtils.dip2px(getContext(), tabBottom) / 2 - PromptUtil.getMeasuredHeight(mBinding.ivGuide) / 2;
            mBinding.ivGuide.setVisibility(View.VISIBLE);
            mBinding.ivGuideTxt.setVisibility(View.VISIBLE);
            mBinding.ivGuide.setPadding(guidePaddingLeft, 0, 0, guidePaddingBottom);
            mBinding.ivGuideTxt.setPadding(guidePaddingLeft, 0, 0, 10);
        }
    };

    private Runnable mMineRunnable = new Runnable() {
        @Override
        public void run() {
            int minePaddingLeft = 7 * deviceWidth / 8 - PromptUtil.getMeasuredWidth(mBinding.ivMine) / 2;
            int minePaddingBottom = DisplayUtils.dip2px(getContext(), tabBottom) / 2 - PromptUtil.getMeasuredHeight(mBinding.ivMine) / 2;
            mBinding.ivMine.setVisibility(View.VISIBLE);
            mBinding.ivMineTxt.setVisibility(View.VISIBLE);
            mBinding.ivMine.setPadding(minePaddingLeft, 0, 0, minePaddingBottom);
            mBinding.ivMineTxt.setPadding(minePaddingLeft, 0, 0, 10);
        }
    };

    private Runnable mBtnRunnable = new Runnable() {
        @Override
        public void run() {
            mBinding.ivBtn.setVisibility(View.VISIBLE);
            mBinding.ivBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    };

    @Override
    protected View onInflateView(LayoutInflater inflater) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.view_prompt_home, null, false);
        mBinding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (View.VISIBLE == mBinding.ivBtn.getVisibility()) {
                    dismiss();
                }
            }
        });
        return mBinding.getRoot();
    }

    @Override
    protected void onInitView(View view) {
        setOnDismissListener(this);
        //头部
        handler.postDelayed(mAvatarRunnable, 500);
        //推荐
        handler.postDelayed(mRecommendRunnable, 1500);
        //发现
        handler.postDelayed(mDiscoverRunnable, 2500);
        //搭配
        handler.postDelayed(mGuideRunnable, 3500);
        //我的
        handler.postDelayed(mMineRunnable, 4500);

        handler.postDelayed(mBtnRunnable, 5000);
    }


    @Override
    public void show() {
        super.show();
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = DisplayUtils.getDeviceWidth(getContext());
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        handler.removeCallbacks(mAvatarRunnable);
        handler.removeCallbacks(mRecommendRunnable);
        handler.removeCallbacks(mDiscoverRunnable);
        handler.removeCallbacks(mGuideRunnable);
        handler.removeCallbacks(mMineRunnable);
        handler.removeCallbacks(mBtnRunnable);
        PromptUtil.setHideHomePrompt(getContext());
    }

    @Override
    public void onBackPressed() {
        if (mCancelable) {
            super.onBackPressed();
        }
    }
}
